package magazijn;
/**
 * Write a description of class test here.
 * 
 * @author (Joep, Jochem, Johannes, Daniel) 
 * @version (v0.1)
 */

import java.sql.*;
public class Database
{
   static private Connection con = null;
   static public void getConnection() throws Exception {
         if ( con == null ) {
            try{
              String userName = "root";
              String password = "usbw";
              String url = "jdbc:mysql://localhost:3307/mediamarkt";
              Class.forName ("com.mysql.jdbc.Driver").newInstance ();
              con = DriverManager.getConnection (url, userName, password);
        
               if(con!=null);
            } catch(Exception e){
               System.out.println("Fouttracering in getConnection() : " + e.getMessage());
               e.printStackTrace();
               throw e;
           }
        }
    }
      
    static ResultSet executeSQLStatement ( String query ) throws Exception {
        try{
            getConnection();
            Statement stmt = con.createStatement();
            stmt.executeQuery( query ); 
            ResultSet resultSet = stmt.getResultSet();
            return resultSet;
        } 
        catch(Exception e){
            System.out.println("Fouttracering in getConnection() : " + e.getMessage());
            e.printStackTrace();
            throw e;
        }
    }
    
    static ResultSet executeUpdateStatement ( String query ) throws Exception {
        try{
            getConnection();
            Statement stmt = con.createStatement();
            stmt.executeUpdate( query ); 
            ResultSet resultSet = stmt.getResultSet();
            return resultSet;
        } 
        catch(Exception e){
            System.out.println("Fout: " + e.getMessage());
            e.printStackTrace();
            throw e;
        }
    }
}


